<!--
 * @Date: 2025-12-01 15:50:18
 * @Description: 柱状图
 * @LastEditTime: 2025-12-01 17:38:28
-->
<template>
  <div class="charts" ref="chartRef"></div>
</template>

<script>
import echarts from 'echarts';
let myCharts = null;
export default {
  name: 'BarChart',
  props: {
    list: { type: Array, default: () => [] },
  },
  data() {
    return {
      chartList: [],
    };
  },
  mounted() {
    myCharts = echarts.init(this.$refs.chartRef);
  },

  watch: {
    list: {
      handler(newVal) {
        this.chartList = newVal;
        this.$nextTick(() => {
          myCharts.setOption(this.getOptions(this.chartList));
        });
      },
      immediate: true,
      deep: true,
    },
  },

  methods: {
    getOptions(data) {
      let xData = [];
      let yData1 = [];
      let yData2 = [];
      if (data.length > 0) {
        data.forEach((item) => {
          xData.push(item.city);
          yData1.push(item.cloud);
          yData2.push(item.wine);
        });
      }

      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        grid: {
          left: '2%',
          right: '4%',
          bottom: '12%',
          top: '16%',
          containLabel: true,
        },
        legend: {
          data: ['云币', '酒币'],
          right: 10,
          top: 5,
          textStyle: {
            color: '#fff',
            fontSize: 12,
          },
          icon: 'rect',
          itemGap: 10,
          itemWidth: 8, // 图例项的宽度
          itemHeight: 8, // 图例项的高度
        },
        xAxis: {
          type: 'category',
          data: xData,
          axisLine: {
            lineStyle: {
              color: '#8CA8B8',
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              fontSize: 12,
              color: '#fff',
              fontFamily: 'Microsoft YaHei',
            },
          },
        },

        yAxis: {
          type: 'value',
          splitNumber: 4, // 设置纵轴分段数为 5
          axisLine: {
            show: false,
            lineStyle: {
              color: '#8CA8B8',
            },
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed', // 设置分割线为虚线
              color: 'rgba(140,168,184,0.3)',
              width: 1,
            },
          },
          axisLabel: {
            show: true, // 保留标签
            color: '#fff', // 标签颜色（如果需要保留标签）
            fontSize: 12,
          },
        },
        series: [
          {
            name: '云币',
            type: 'bar',
            barWidth: '10',
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#4390FF',
                  },
                  {
                    offset: 1,
                    color: '#0A00FF',
                  },
                ]),
              },
            },
            data: yData1,
          },
          {
            name: '酒币',
            type: 'bar',
            barWidth: '10',
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#FF7038',
                  },
                  {
                    offset: 1,
                    color: '#C7293F',
                  },
                ]),
              },
            },
            data: yData2,
          },
        ],
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.charts {
  width: 100%;
  height: 100%;
}
</style>
